package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.MemberMapper;
import com.itheima.mapper.OrderMapper;
import com.itheima.mapper.ReportMapper;
import com.itheima.service.ReportService;
import com.itheima.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/*
    运营数据服务
 */
@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceImpl implements ReportService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ReportMapper reportMapper;

    //查询运营数据
    public Map<String, Object> getBusinessReportData() throws Exception {
        //报表日期
        String today = DateUtils.parseDate2String(DateUtils.getToday());
        //获得本周一日期
        String thisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
        //获得本月第一天日期
        String firstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
        //本日新增会员数
        Integer todayNewMember = memberMapper.findMemberCountByDate(today);
        //总会员数
        Integer totalMember = memberMapper.findMemberTotalCount();
        //本周新增会员数
        Integer thisWeekNewMember = memberMapper.findMemberCountAfterDate(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember = memberMapper.findMemberCountAfterDate(firstDay4ThisMonth);
        //今日预约数
        Integer todayOrderNumber = orderMapper.findOrderCountByDate(today);
        //本周预约数
        Integer thisWeekOrderNumber = orderMapper.findOrderCountByDate(thisWeekMonday);
        //本月预约数
        Integer thisMonthOrderNumber = orderMapper.findOrderCountByDate(firstDay4ThisMonth);
        //今日到诊数
        Integer todayVisitsNumber = orderMapper.findVisitsCountAfterDate(today);
        //本周到诊数
        Integer thisWeekVisitsNumber = orderMapper.findVisitsCountAfterDate(thisWeekMonday);
        //本月到诊数
        Integer thisMonthVisitsNumber = orderMapper.findVisitsCountAfterDate(firstDay4ThisMonth);
        //热门套餐查询
        List<Map> hotSetmeal = orderMapper.findHotSetmeal();
        Map<String,Object> result = new HashMap<>();
        result.put("reportDate",today);
        result.put("todayNewMember",todayNewMember);
        result.put("totalMember",totalMember);
        result.put("thisWeekNewMember",thisWeekNewMember);
        result.put("thisMonthNewMember",thisMonthNewMember);
        result.put("todayOrderNumber",todayOrderNumber);
        result.put("thisWeekOrderNumber",thisWeekOrderNumber);
        result.put("thisMonthOrderNumber",thisMonthOrderNumber);
        result.put("todayVisitsNumber",todayVisitsNumber);
        result.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
        result.put("thisMonthVisitsNumber",thisMonthVisitsNumber);
        result.put("hotSetmeal",hotSetmeal);
        return result;
    }

    @Override
    public List<String> findByBirthdays() {
        return reportMapper.findByBirthdays();
    }

    @Override
    public List<Integer> findtoOrderStatus(ArrayList<String> dayList) {
        List<Integer> map = new ArrayList<>();
        /*预约数*/
        for (String s : dayList) {
            Integer visitsCountByDate = reportMapper.findVisitsCountByDate(s);
            map.add(visitsCountByDate);
        }
        return map;
    }

    @Override
    public List<Integer> findtoOrderList(ArrayList<String> dayList) {
        List<Integer> map = new ArrayList<>();
        for (String s1 : dayList) {
            Integer visitsCountAfterDate1 = reportMapper.findVisitsCountAfterDate(s1);
            map.add(visitsCountAfterDate1);
        }
        return map;
    }

    //查询所有套餐名称
    public List<String> findSetmealNameCount() {
        return reportMapper.findSetmealNameCount();
    }

    //查询每个套餐的id
    public Integer findnameById(String name) {
        return reportMapper.findnameById(name);
    }

    /**
     * 每个套餐的金额
     * @param id
     * @return
     */
    public Integer findSetmealMoney(Integer id) {
        return reportMapper.findSetmealMoney(id);
    }

    /**
     *
     * @param id
     * @param dayList
     * @return
     */
    public Double findSetmealNum(Integer id, ArrayList<String> dayList) {
        Double n = 0.0;
        for (String s : dayList) {
            //每天已到诊的预约套餐数量
            Integer num=reportMapper.findSetmealNum(id,s);
            n += num;
        }
        return n;
    }
}
